﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Model;
namespace LaKuaShop_TestDemo
{
    /// <summary>
    /// Paying.xaml 的交互逻辑
    /// </summary>
    public partial class Paying : Window
    {
        List<string> pids = new List<string>();
        List<int> nums = new List<int>();
        string Sum;
        MainWindow win2;
        //List<ProductInfo> products;
        public Paying(List<string> pid, List<int> num, string sum, MainWindow win1)
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            //Pid = pid;
            pids = pid;
            nums = num;
            Sum = sum;
            IniPay();
            win2 = win1;
        }

        void IniPay()
        {
            payment.Text = Sum;
            WeiXin.IsChecked = true;
            //改到结算页面明天
        }
        //确认支付按钮
        private void OKPaying_Click(object sender, RoutedEventArgs e)
        {
            //SucessPay w1 = new SucessPay();
            //this.Close();
            //w1.ShowDialog();
        }
        //关闭按钮
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //立即付款按钮
        private void Pay_ButtonClick(object sender, RoutedEventArgs e)
        {
            if (PayPass.Password == "")
            {
                MessageBox.Show("请输入支付密码！");
                return;
            }
            BLL.Order Order = new BLL.Order();
            //添加订单记录
            string uuid = System.Guid.NewGuid().ToString("N");
            OrderInfo orderinfo = new OrderInfo(uuid, Common.User.account, pids, DateTime.Today, nums);
            Order.AddOrder(orderinfo);

            //删除购物车记录
            BLL.BuyCart BuyCart = new BLL.BuyCart();
            for (int i = 0; i < pids.Count; i++)
            {
                BuyCart.SubOneBuyCart(Common.User.account, pids[i], nums[i]);
            }

            //减少库存
            BLL.Product Product = new BLL.Product();
            List<ProductInfo> products = new List<ProductInfo>();
            for (int i = 0; i < pids.Count; i++)
            {
                products.Add(Product.ShowProductInfo(pids[i]));
                products[i].Pnum -= nums[i];
                Product.UpdateProduct(products[i].Pid, products[i]);
            }
            SucessPay w1 = new SucessPay(Sum,win2);
            this.Close();
            w1.ShowDialog();
        }
    }
}
